unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls, RzTrkBar;

type
  TForm1 = class(TForm)
    Input: TEdit;
    Start: TButton;
    Progress: TProgressBar;
    Out: TLabel;
    Image1: TImage;
    tch: TRzTrackBar;
    procedure StartClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure tchChange(Sender: TObject);
    procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Temp: TBitMap;
  MMove: boolean;

implementation

{$R *.dfm}

procedure TForm1.StartClick(Sender: TObject);
var
  Nn, N, NE : Cardinal;
  X, Y: Extended;
begin
  If StrToInt( input.Text ) < 660  then begin
    ShowMessage( 'number wich you have entered is too small.' + #13#10 +
                 'Please enter a value greater than 660.' );
    Exit;
  end;
  Image1.Canvas.Draw( 0, 0, Temp );
  MMove := false;
  tch.Enabled := MMove;
  Start.Enabled := MMove;
  Nn := StrToInt( Input.Text );
  N := 0;
  NE := 0;
  Progress.Position := 0;
  Image1.Canvas.Pen.Color := clRed;
  Image1.Canvas.MoveTo( 37, 200 );
  Image1.Canvas.LineTo( 697, 200 );
  Image1.Canvas.MoveTo( 37, 200 );
  //// First
    N := 1;
    X := 2*random;
    Y := 2*random;
    Inc( N );
    If ( sqr( x - 1 ) + sqr( y - 1 ) <= 1 ) then
      Inc( NE );
    If N mod ( Nn div 660 ) = 0 then begin
      Progress.Position := round( ( N/Nn )*100 );
      Out.Caption := 'Pi = ' + FloatToStr( 4*NE/N );
      Image1.Canvas.LineTo( round( ( N / Nn )*660 ) + 37, round( ( pi - (4*NE/N) )*200*tch.Position ) + 200 );
      Application.ProcessMessages;
    end;
  //////////
  while N < Nn do begin
    X := 2*random;
    Y := 2*random;
    Inc( N );
    If ( sqr( x - 1 ) + sqr( y - 1 ) <= 1 ) then
      Inc( NE );
    If N mod ( Nn div 660 ) = 0 then begin
      Progress.Position := round( ( N/Nn )*100 );
      Out.Caption := 'Pi = ' + FloatToStr( 4*NE/N );
      Image1.Canvas.LineTo( round( ( N / Nn )*660 ) + 37, round( ( pi - 4*NE/N )*200*tch.Position ) + 200 );
      Application.ProcessMessages;
    end;
  end;
  Out.Caption := 'Pi = ' + FloatToStr( 4*NE/N );
  temp.Canvas.CopyRect( image1.ClientRect, image1.Canvas, image1.ClientRect );
  MMove := true;
  tch.Enabled := MMove;
  Start.Enabled := MMove;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  randomize;
  Temp := TBitMap.Create;
  Temp.Width := Image1.Width;
  Temp.Height := Image1.Height;
  tchChange( sender );
  MMove := true;
end;

procedure TForm1.tchChange(Sender: TObject);
var i, k: integer;
begin
  Image1.Canvas.Draw( 0, 0, Temp );
  image1.Canvas.Pen.Color := clWhite;
  image1.Canvas.Rectangle( 0, 0 , 36, image1.Height );
  k := Image1.Height div 11;
  for i := 0 to 10 do begin
    image1.Canvas.TextOut( 1, 6 + ( k*i ), FloatToStr( (round((1-i/5)*1000/tch.Position))/1000 ) );
    image1.Canvas.Pen.Color := clLime;
    image1.Canvas.MoveTo( 1, 20 + ( k*i ) );
    image1.Canvas.LineTo( 32, 20 + ( k*i ) );
  end;
  temp.Canvas.CopyRect( image1.ClientRect, image1.Canvas, image1.ClientRect );
  Application.ProcessMessages;
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  i, j, t: integer;
begin
  if MMove then begin
    Image1.Canvas.Pen.Color := ClBlue;
    Image1.Canvas.Draw( 0, 0, Temp );
    image1.Canvas.MoveTo( x, y );
    image1.Canvas.LineTo( 32, y );
    //t := Image1.Canvas.Font.Size;
    //Image1.Canvas.Font.Name := 'Terminal';
    //Image1.Canvas.Font.Size := 1;
    //Image1.Canvas.TextOut( x, y - 10, FloatToStr( -( y - 200 )/( 200*tch.Position ) ) );
    //Image1.Canvas.Font.Size := t;
  end;
end;

end.
